import createVanilla from 'zustand/vanilla'
import { persist, subscribeWithSelector } from 'zustand/middleware'
import create from 'zustand'
import { returnTypeOperate } from '../../components'
import { ActionButtonsType } from './actionButtons/translator'
import { Subject } from 'rxjs'

export type tabType = 'all' | 'often' | 'interactive' | 'dcs' | 'live'

export const TAB = {
	all: {
		canShow: true,
		onlineSize: 0,
		size: 0,
	},
	often: {
		canShow: true,
		onlineSize: 0,
		size: 0,
	},
	interactive: {
		canShow: false,
		onlineSize: 0,
		size: 0,
	},
	dcs: {
		canShow: false,
		onlineSize: 0,
		size: 0,
	},
	live: {
		canShow: false,
		size: 0,
		guestSize: 0,
	},
}
type StateStore = {
	/**
	 * 搜索出来的关键字
	 */
	keywords: string
	/**
	 * 列表排序 1-呼叫顺序 2-在离线顺序 3-组织架构
	 * (初始化采用个性化配置)
	 */
	tableSort: 1 | 2 | 3
	/**
	 *  当前组织架构级联树的选择项 (保留作为后续更新的不变项)
	 */
	currentOrgId: string
	/**
	 *  tab参数
	 */
	tab: typeof TAB
	/**
	 * 当前 tab
	 */
	currentTab: tabType
	/**
	 * vip列表顺序，mtKey拼接而成
	 */
	vipSort: string
	/**
	 *  列表项的操作按钮
	 */
	tableItemAction: returnTypeOperate
	/**
	 *  会控右侧打开需要的底部按钮集
	 * 不随着tab等切换而随意改变，类似于缓存
	 * 实际界面显示使用actionButton store 里的 screenList
	 */
	actionButtonList: ActionButtonsType[]
	/**
	 * 列表 宽度
	 */
	tableWidth: {
		nameWidth: number
		operateWidth: number
		statusWidth: number
	}
	/**
	 *  触发内部宽度刷新
	 */
	refreshFlag: 0 | 1
}
type IStore = StateStore & {
	update: (d: Partial<StateStore>) => void
	reset: () => void
	superMeetingTab: () => void
	refreshTableWidth: () => void
	hideActionButton: () => void
	showActionButton: (type?: 'hangUpAllMt' | 'deleteAllMt' | 'mtVideo') => void
}
const initialState: StateStore = {
	keywords: '',
	tab: TAB,
	tableSort: 1,
	currentOrgId: 'all',
	currentTab: 'all',
	vipSort: '',
	tableItemAction: {
		mt: { icon: [], dropDown: [] },
		subMt: { icon: [], dropDown: [] },
		meeting: { icon: [], dropDown: [] },
	},
	actionButtonList: [],
	tableWidth: {
		nameWidth: 0,
		operateWidth: 0,
		statusWidth: 0,
	},
	refreshFlag: 0,
}
export const paramsRx = {
	tableSort$: new Subject(),
	/**
	 *  列表项 单个终端呼叫
	 */
	tableItemCalling$: new Subject<string>(),
	/**
	 *  列表项 全部呼叫
	 */
	tableCalling$: new Subject(),
}
export const store = createVanilla<IStore>()(
	subscribeWithSelector(
		persist(
			(set, get) => ({
				...initialState,
				update: (d) => set({ ...d }),
				reset: () => set(initialState),
				superMeetingTab: () => {
					const prevTab = get().tab
					prevTab.often.canShow = false
					prevTab.interactive.canShow = true
					set({
						tab: { ...prevTab },
					})
				},
				refreshTableWidth: () => {
					set((v) => ({ ...v, refreshFlag: (v.refreshFlag ^ 1) as 0 | 1 }))
				},
				hideActionButton: () => {
					set({ actionButtonList: [] })
				},
				showActionButton: (type) => {
					switch (type) {
						case 'hangUpAllMt':
							set({
								actionButtonList: ['hangUp', 'checkAll', 'cancel'],
							})
							break
						case 'deleteAllMt':
							set({
								actionButtonList: ['delete', 'checkAll', 'cancel'],
							})
							break
						case 'mtVideo':
							set({
								actionButtonList: ['startMtVmp', 'createMtVmp', 'checkAll'],
							})
							break
						default:
							set({
								actionButtonList: ['checkAll', 'add'],
							})
					}
				},
			}),
			{
				name: 'mcc_table_width',
				partialize: (state) => ({
					tableWidth: state.tableWidth,
				}),
			}
		)
	)
)
export const useParamsStore = create(store)
export function getCurrentTabType() {
	const { tableSort, currentTab, currentOrgId } = useParamsStore.getState()
	let tableType: 'all' | 'often' | 'org' | 'interactive' | 'dcs' | 'live' = 'all'
	if (currentTab === 'all' && tableSort !== 3) {
		tableType = 'all'
	} else if ((tableSort === 3 && currentOrgId === 'often') || (currentTab === 'often' && tableSort !== 3)) {
		tableType = 'often'
	} else if (
		(tableSort === 3 && currentOrgId === 'interactive') ||
		(currentTab === 'interactive' && tableSort !== 3)
	) {
		tableType = 'interactive'
	} else if ((tableSort === 3 && currentOrgId === 'dcs') || (currentTab === 'dcs' && tableSort !== 3)) {
		tableType = 'dcs'
	} else if ((tableSort === 3 && currentOrgId === 'live') || (currentTab === 'live' && tableSort !== 3)) {
		tableType = 'live'
	} else if (
		tableSort === 3 &&
		currentOrgId !== 'live' &&
		currentOrgId !== 'often' &&
		currentOrgId !== 'dcs' &&
		currentOrgId !== 'interactive'
	) {
		tableType = 'org'
	}
	return tableType
}
